﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Security.Permissions;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using EF;

namespace test
{
    class Program
    {
        public string _strJobItemName = string.Empty;
        public bool _bAppendHtmlTag = false;
        public bool _bHtmlEncode = false;

        public static string AddWebPartPage(string strFileName, string strFileTitle, SPWeb objSPWeb, SPList objSPList, int iWebPartPageTemplate, string strPageType, string strFolderPath)
        {
            SPFile objSPFile = null;
            string postInformation =
            @"<?xml version=""1.0"" encoding=""UTF-8""?>" +
            @"<Method>" +
            @"<SetList Scope=""Request"">" + objSPList.ID + @"</SetList>" +
            @"<SetVar Name=""ID"">New</SetVar>" +
            @"<SetVar Name=""Cmd"">NewWebPage</SetVar>" +
            @"<SetVar Name=""Type"">" + strPageType + @"</SetVar>" +
            @"<SetVar Name=""WebPartPageTemplate"">" + iWebPartPageTemplate + @"</SetVar>" +
            @"<SetVar Name=""Title"">" + strFileName + @"</SetVar>" +
            @"<SetVar Name=""Overwrite"">true</SetVar>" +
            @"</Method>";
            string processBatch = objSPWeb.ProcessBatchData(postInformation);
            if (processBatch.Equals("<Result ID=\"\" Code=\"0\">\r\n</Result>\n", StringComparison.InvariantCultureIgnoreCase))
            {
                string strFileUrl = objSPList.RootFolder.Url + "/" + strFileName;
                if (strFileUrl.EndsWith(".aspx") == false)
                {
                    strFileUrl += ".aspx";
                }
                objSPFile = objSPWeb.GetFile(strFileUrl);
                //if page was in subfolder then move it there
                if (!String.IsNullOrEmpty(strFolderPath))
                {
                    objSPFile.MoveTo(strFileTitle, true);
                }
                SPFolder rootFolder = objSPFile.ParentFolder;

                using (SPLimitedWebPartManager spLimitedWebPartManager = objSPWeb.GetLimitedWebPartManager(strFileUrl, PersonalizationScope.Shared))
                {

                    foreach (System.Web.UI.WebControls.WebParts.WebPart objWebpart in spLimitedWebPartManager.WebParts)
                    {
                        if (objWebpart.ToString() == "Microsoft.SharePoint.WebPartPages.TitleBarWebPart")
                        {
                            TitleBarWebPart objTitleBarWebPart = (TitleBarWebPart)objWebpart;
                            objTitleBarWebPart.HeaderTitle = strFileTitle;
                            spLimitedWebPartManager.SaveChanges(objTitleBarWebPart);
                            break;
                        }
                    }
                    //System.Web.UI.WebControls.WebParts.WebPart objWebPart = spLimitedWebPartManager.WebParts["TitleBar"];
                    //objWebPart.Title = strFileTitle;
                    objSPWeb.Update();
                }

                return objSPFile.Url;
            }

            return string.Empty;
        }

        public static System.Web.UI.WebControls.WebParts.WebPart CreateWebPart(SPWeb web, string webPartName)
        {
            SPQuery query = new SPQuery();
            query.Query = String.Format(
                "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>{0}</Value></Eq></Where>",
                webPartName);

            SPList webPartGallery = null;

            if (null == web.ParentWeb)
            {
                // This is the root web.
                webPartGallery = web.GetCatalog(SPListTemplateType.WebPartCatalog);
            }
            else
            {
                // This is a sub-web.
                webPartGallery = web.ParentWeb.GetCatalog(SPListTemplateType.WebPartCatalog);
            }

            SPListItemCollection webParts = webPartGallery.GetItems(query);

            string typeName = webParts[0].GetFormattedValue("WebPartTypeName");
            string assemblyName = webParts[0].GetFormattedValue("WebPartAssembly");
            ObjectHandle webPartHandle = Activator.CreateInstance(assemblyName, typeName);

            System.Web.UI.WebControls.WebParts.WebPart webPart =
                (System.Web.UI.WebControls.WebParts.WebPart)webPartHandle.Unwrap();
            return webPart;
        }

        public static object ConvertValue(string propertyValue, PropertyInfo property)
        {
            if (propertyValue != string.Empty)
            {
                object value = Convert.ChangeType(propertyValue, property.PropertyType);
                return value;
            }

            return null;
        }

        public static string getFullJobName(string strUrl)
        {
            return getFullJobName(strUrl, string.Empty);
        }

        public static string getFullJobName(string strUrl, string strRecurringType)
        {
            string strReturn = strUrl;
            strReturn = strReturn.Substring(strReturn.IndexOf(@"//") + 2);
            strReturn = strReturn.Replace(@":", @"_");
            strReturn = strReturn.Replace(@"/", @"_");
            strReturn = string.Format(@"{0}{1}{2}", EFSimpleReminder.JOB_NAME, EFSimpleReminder.JobName_SeparatorChar, strReturn);

            if (string.IsNullOrEmpty(strRecurringType) == false)
            {
                strReturn += string.Format("{0}{1}", EFSimpleReminder.JobName_SeparatorChar, strRecurringType);
            }

            return strReturn;
        }

        public static void SetWebPartProperty(
            SPWeb web,
            string pageUrl,
            string webPartID,
            string propertyName,
            string propertyValue)
        {
            using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
            {
                System.Web.UI.WebControls.WebParts.WebPart part = manager.WebParts[webPartID];
                Type runtimeType = part.GetType();
                PropertyInfo property = runtimeType.GetProperty(propertyName);
                object value = ConvertValue(propertyValue, property);
                property.SetValue(part, value, null);
                manager.SaveChanges(part);
            }
        }

        public static void AddWebPartConnection(
            SPWeb web,
            string pageUrl,
            string providerWebPartID,
            string consumerWebPartID,
            string providerConnectionPointName,
            string consumerConnectionPointName)
        {
            using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
            {
                System.Web.UI.WebControls.WebParts.WebPart provider = manager.WebParts[providerWebPartID];
                System.Web.UI.WebControls.WebParts.WebPart consumer = manager.WebParts[consumerWebPartID];

                ProviderConnectionPointCollection providerPoints = manager.GetProviderConnectionPoints(provider);
                ConsumerConnectionPointCollection consumerPoints = manager.GetConsumerConnectionPoints(consumer);

                ProviderConnectionPoint providerPoint = null;

                foreach (ProviderConnectionPoint point in providerPoints)
                {
                    if (String.Equals(providerConnectionPointName, point.DisplayName, StringComparison.OrdinalIgnoreCase))
                    {
                        providerPoint = point;
                        break;
                    }
                }

                ConsumerConnectionPoint consumerPoint = null;

                foreach (ConsumerConnectionPoint point in consumerPoints)
                {
                    if (String.Equals(consumerConnectionPointName, point.DisplayName, StringComparison.OrdinalIgnoreCase))
                    {
                        consumerPoint = point;
                        break;
                    }
                }

                manager.SPConnectWebParts(provider, providerPoint, consumer, consumerPoint);
            }
        }

        //public static void SetToolbarTypeFreeForm(
        //    ListViewWebPart lvwp,
        //    SPLimitedWebPartManager limitedWebPartManager,
        //    EFSimpleReminder.ListViewWebPartToolbarType type)
        public static void SetToolbarTypeFreeForm(SPWeb objSPWeb, string strPageUrl, Guid guidStorageKey)
        {
            string storageKey = guidStorageKey.ToString("B").ToUpper();
            uint toolbarType = (uint)EFSimpleReminder.ListViewWebPartToolbarType.FreeForm;

            if (String.IsNullOrEmpty(storageKey) || storageKey == Guid.Empty.ToString())
                throw new Exception("The webpart must be added to the page before this method (SetToolbarType) can be called");

            SPLimitedWebPartManager limitedWebPartManager = null;
            limitedWebPartManager = objSPWeb.GetLimitedWebPartManager(strPageUrl, PersonalizationScope.Shared);

            ListViewWebPart lvwp = null;
            lvwp = (ListViewWebPart)limitedWebPartManager.WebParts[guidStorageKey];

            string listId = lvwp.ListName;
            string viewId = lvwp.ViewGuid;

            // get the SPWebPartManager from SPLimitedWebPartManager
            PropertyInfo webPartManagerProp = typeof(SPLimitedWebPartManager).GetProperty(
              "WebPartManager",
              BindingFlags.NonPublic | BindingFlags.Instance);
            SPWebPartManager webPartManager = (SPWebPartManager)webPartManagerProp.GetValue(limitedWebPartManager, null);

            // get the SPWebPartConnection from SPWebPartManager
            PropertyInfo spWebPartsProp = typeof(SPWebPartManager).GetProperty(
              "SPWebParts",
              BindingFlags.NonPublic | BindingFlags.Instance);
            SPWebPartCollection webParts = (SPWebPartCollection)spWebPartsProp.GetValue(webPartManager, null);

            // Call the ApplyViewToListWebPart method on the SPWebPartConnection
            // internal void ApplyViewToListWebPart(
            //    string storageKey,
            //    string listID,
            //    string viewID,
            //    uint toolbarType,
            //    out uint flags,
            //    out uint baseViewId)
            MethodInfo applyViewToListWebPart = typeof(SPWebPartCollection).GetMethod(
              "ApplyViewToListWebPart",
              BindingFlags.NonPublic | BindingFlags.Instance,
              null,
              new[]{
        typeof(string),
        typeof(string),
        typeof(string),
        typeof(uint),
        typeof(uint).MakeByRefType(),
        typeof(uint).MakeByRefType()
      },
              null);
            object[] parameters = new object[6];
            parameters[0] = storageKey;
            parameters[1] = listId;
            parameters[2] = viewId;
            parameters[3] = toolbarType;

            applyViewToListWebPart.Invoke(webParts, parameters);
        }

        public static void ShowToolbar(SPWeb objSPWeb, string strPageUrl, Guid guidStorageKey)
        {
            SPLimitedWebPartManager manager = null;
            manager = objSPWeb.GetLimitedWebPartManager(strPageUrl, PersonalizationScope.Shared);

            object[] setToolbarTypeParams = { (uint)EFSimpleReminder.ListViewWebPartToolbarType.ShowToolbar };
            ListViewWebPart objListViewWebPart = null;
            objListViewWebPart = (ListViewWebPart)manager.WebParts[guidStorageKey];

            PropertyInfo viewProp = objListViewWebPart.GetType().GetProperty("View", BindingFlags.Instance | BindingFlags.NonPublic);
            SPView webPartView = viewProp.GetValue(objListViewWebPart, null) as SPView;

            MethodInfo setToolbarType = webPartView.GetType().GetMethod("SetToolbarType",
                BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(uint) }, null);
            setToolbarType.Invoke(webPartView, setToolbarTypeParams);

            webPartView.Update();
            manager.SaveChanges(objListViewWebPart);
        }

        public static void initAdminPage(SPSite site)
        {
            SPDocumentLibrary spDocPageLib = null;

            SPWeb objSPWeb = site.RootWeb;
            Guid guidSitePages = Guid.Empty;
            SPList objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.DocLib_SitePages_Title);
            if (objSPList == null)
            {
                guidSitePages = objSPWeb.Lists.Add(EFSimpleReminder.DocLib_SitePages_PathName, "", SPListTemplateType.DocumentLibrary);
                objSPList = objSPWeb.Lists[guidSitePages];
                objSPList.Title = EFSimpleReminder.DocLib_SitePages_Title;
                objSPList.Update();
            }
            else
            {
                guidSitePages = objSPList.ID;
                foreach (SPListItem item in objSPList.Items)
                {
                    if (item.File.Name.StartsWith(EFSimpleReminder.AdminPage_Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        item.Delete();
                        break;
                    }
                }
            }
            spDocPageLib = (SPDocumentLibrary)objSPWeb.Lists[guidSitePages];

            string strPageUrl = AddWebPartPage(EFSimpleReminder.AdminPage_Name, "EF SimpleReminder Admin Page", objSPWeb, spDocPageLib, 2, "WebPartPage", "");
            SPFile currentWebPartPage = objSPWeb.GetFile(strPageUrl);

            string strListUrl = string.Empty;
            ListViewWebPart objListViewWebPart = null;
            SPLimitedWebPartManager manager = null;

            try
            {
                objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.Configuration_ListTitle);
                if (objSPList == null)
                    throw new ArgumentException("List(" + EFSimpleReminder.Configuration_ListTitle + ") not found.");
                Console.WriteLine(string.Format(@"ConfigurationView.Title={0}, ConfigurationView.Url={1}", objSPList.DefaultView.Title, objSPList.DefaultView.Url));
                objListViewWebPart = AddListViewToPage(objSPWeb, strPageUrl, objSPList.DefaultView, EFSimpleReminder.Zone_Header, 1, @"Scheduled reminder list");
                ShowToolbar(objSPWeb, strPageUrl, objListViewWebPart.StorageKey);
                SetToolbarTypeFreeForm(objSPWeb, strPageUrl, objListViewWebPart.StorageKey);
                //SetToolbarType(objListViewWebPart, manager, EFSimpleReminder.ListViewWebPartToolbarType.Standard);
                //SetToolbarType(objListViewWebPart, manager, EFSimpleReminder.ListViewWebPartToolbarType.ShowToolbar);

                objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.History_ListTitle);
                if (objSPList == null)
                    throw new ArgumentException("List(" + EFSimpleReminder.History_ListTitle + ") not found.");
                Console.WriteLine(string.Format(@"HistoryView.Title={0}, HistoryView.Url={1}", objSPList.Views[EFSimpleReminder.View_Portal].Title, objSPList.Views[EFSimpleReminder.View_Portal].Url));
                objListViewWebPart = AddListViewToPage(objSPWeb, strPageUrl, objSPList.Views[EFSimpleReminder.View_Portal], EFSimpleReminder.Zone_Footer, 1, @"Reminder history");
                ShowToolbar(objSPWeb, strPageUrl, objListViewWebPart.StorageKey);

                objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.TaskDefault_ListTitle);
                if (objSPList == null)
                    throw new ArgumentException("List(" + EFSimpleReminder.TaskDefault_ListTitle + ") not found.");
                Console.WriteLine(string.Format(@"TaskView.Title={0}, TaskView.Url={1}", objSPList.Views[EFSimpleReminder.View_Portal].Title, objSPList.Views[EFSimpleReminder.View_Portal].Url));
                objListViewWebPart = AddListViewToPage(objSPWeb, strPageUrl, objSPList.Views[EFSimpleReminder.View_Portal], EFSimpleReminder.Zone_LeftColumn, 1, @"Reminder Tasks");
                //changeToolbarType(objSPWeb, strPageUrl, objListViewWebPart.StorageKey, EFSimpleReminder.ListViewWebPartToolbarType.ShowToolbar);

                objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.Job_ListTitle);
                if (objSPList == null)
                    throw new ArgumentException("List(" + EFSimpleReminder.Job_ListTitle + ") not found.");
                Console.WriteLine(string.Format(@"JobView.Title={0}, JobView.Url={1}", objSPList.Views[EFSimpleReminder.View_Portal].Title, objSPList.Views[EFSimpleReminder.View_Portal].Url));
                objListViewWebPart = AddListViewToPage(objSPWeb, strPageUrl, objSPList.Views[EFSimpleReminder.View_Portal], EFSimpleReminder.Zone_RightColumn, 1, @"Reminder Jobs");

            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format(@"ex.Message={0}", ex.Message));
                Console.WriteLine(string.Format(@"ex.StackTrace={0}", ex.StackTrace));

                throw;
            }
        }

        //  http://neganov.blogspot.com/2007/11/add-web-part-to-sharepoint-page.html
        public static ListViewWebPart AddListViewToPage(
            SPWeb objSPWeb,
            string pageUrl,
            SPView objSPView,
            string zoneID,
            int zoneIndex,
            string strWebPartTitle)
        {
            ListViewWebPart objListViewWebPart = null;
            string strListUrl = string.Empty;
            string strListViewWebPartID = string.Empty;
            if (string.IsNullOrEmpty(strWebPartTitle))
                strWebPartTitle = objSPView.ParentList.Title;

            using (SPLimitedWebPartManager spLimitedWebPartManager = objSPWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
            {
                using (objListViewWebPart = new ListViewWebPart())
                {
                    objListViewWebPart.ListName = objSPView.ParentList.ID.ToString("B").ToUpperInvariant();
                    objListViewWebPart.TitleUrl = objSPView.ServerRelativeUrl;
                    objListViewWebPart.WebId = objSPWeb.ID;
                    objListViewWebPart.Title = strWebPartTitle;
                    objListViewWebPart.ViewGuid = objSPView.ID.ToString();
                    //objListViewWebPart.DisplayTitle = "abcdefg";

                    spLimitedWebPartManager.AddWebPart(objListViewWebPart, zoneID, zoneIndex);

                    foreach (System.Web.UI.WebControls.WebParts.WebPart objWebpart in spLimitedWebPartManager.WebParts)
                    {
                        if (objWebpart.TitleUrl == objSPView.ServerRelativeUrl)
                        {
                            return (ListViewWebPart)objWebpart;
                        }
                    }
                }

                //return strListViewWebPartID;
                return null;
            }
        }

        public static void initJobs(SPSite objSPSite)
        {
            string strJobName = string.Empty;
            string strJobUrl = string.Empty;
            SPList splistJob = objSPSite.RootWeb.Lists.TryGetList(EFSimpleReminder.Job_ListTitle);
            SPListItem objSPListItemJob = null;

            string[] strArrayRecurringType = { EFSimpleReminder.RecurringType_Monthly, EFSimpleReminder.RecurringType_Weekly, EFSimpleReminder.RecurringType_Daily, EFSimpleReminder.RecurringType_Hourly, EFSimpleReminder.RecurringType_Minutes };
            foreach (string strRecurringType in strArrayRecurringType)
            {
                strJobName = getFullJobName(objSPSite.Url, strRecurringType);

                // make sure the job isn't already registered
                foreach (SPJobDefinition job in objSPSite.WebApplication.JobDefinitions)
                {
                    //if (job.Name.StartsWith(JOB_NAME, StringComparison.InvariantCultureIgnoreCase))
                    if (job.Name.Equals(strJobName, StringComparison.InvariantCultureIgnoreCase))
                        job.Delete();
                }

                EFSimpleReminder listLoggerJob = new EFSimpleReminder(strJobName, objSPSite.WebApplication);

                switch (strRecurringType)
                {
                    case EFSimpleReminder.RecurringType_Monthly:
                        SPMonthlyByDaySchedule scheduleMonthly = new SPMonthlyByDaySchedule();
                        scheduleMonthly.BeginWeek = WeekOfMonth.First;
                        scheduleMonthly.BeginDay = DayOfWeek.Monday;
                        scheduleMonthly.BeginHour = 1;
                        scheduleMonthly.BeginMinute = 0;
                        listLoggerJob.Schedule = scheduleMonthly;
                        break;
                    case EFSimpleReminder.RecurringType_Weekly:
                        SPWeeklySchedule scheduleWeekly = new SPWeeklySchedule();
                        scheduleWeekly.BeginDayOfWeek = DayOfWeek.Monday;
                        scheduleWeekly.BeginHour = 0;
                        scheduleWeekly.BeginMinute = 0;
                        scheduleWeekly.EndDayOfWeek = DayOfWeek.Monday;
                        scheduleWeekly.EndHour = 1;
                        scheduleWeekly.EndMinute = 59;
                        listLoggerJob.Schedule = scheduleWeekly;
                        break;
                    case EFSimpleReminder.RecurringType_Daily:
                        SPDailySchedule scheduleDaily = new SPDailySchedule();
                        scheduleDaily.BeginHour = 0;
                        scheduleDaily.EndHour = 1;
                        listLoggerJob.Schedule = scheduleDaily;
                        break;
                    case EFSimpleReminder.RecurringType_Hourly:
                        SPHourlySchedule scheduleHourly = new SPHourlySchedule();
                        scheduleHourly.BeginMinute = 0;
                        scheduleHourly.EndMinute = 10;
                        listLoggerJob.Schedule = scheduleHourly;
                        break;
                    case EFSimpleReminder.RecurringType_Minutes:
                        SPMinuteSchedule scheduleMinutes = new SPMinuteSchedule();
                        scheduleMinutes.Interval = 10;
                        listLoggerJob.Schedule = scheduleMinutes;
                        break;
                    default:
                        Console.WriteLine(string.Format(@"initJobs(), invalid strRecurringType={0}, strJobName={1}", strRecurringType, strJobName));
                        break;
                }

                listLoggerJob.Update(true);

                if (splistJob != null)
                {
                    objSPListItemJob = splistJob.AddItem();
                    objSPListItemJob[SPBuiltInFieldId.Title] = strJobName;
                    //   http://hvsp2010a:9000/_admin/JobEdit.aspx?JobId=c936e330%2D54a7%2D4314%2D9a68%2D3178934638dc
                    SPAdministrationWebApplication centralWebApp = SPAdministrationWebApplication.Local;
                    strJobUrl = string.Format("{0}/_admin/JobEdit.aspx?JobId={1}&Source={2}", centralWebApp.Sites[0].Url, listLoggerJob.Id, SPEncode.UrlEncode(objSPSite.Url + @"/" + EFSimpleReminder.AdminPage_Url));
                    //logInfo(string.Format(@"strJobName={0}, JobUrl={1}", strJobName, strJobUrl));
                    objSPListItemJob[EFSimpleReminder.Column_EFSimpleReminderJob_AdminLink] = string.Format("{0}, {1}", strJobUrl, strRecurringType);
                    objSPListItemJob.Update();
                }

                Console.WriteLine(string.Format(@"Job ({0}) created.", listLoggerJob.Title));
            }
        }

        public static void initLists(SPSite site)
        {
            SPList splistConfiguration = null;
            SPList splistHistory = null;
            SPList splistTask = null;
            SPList splistJob = null;

            SPView oViewConfiguration = null;
            SPViewFieldCollection oViewFieldsConfiguration = null;

            SPView oViewHistory = null;
            SPViewFieldCollection oViewFieldsHistory = null;

            SPView oViewTask = null;
            SPViewFieldCollection oViewFieldsTask = null;

            SPView oViewJob = null;
            SPViewFieldCollection oViewFieldsJob = null;

            string strViewQuery = string.Empty;
            string strViewQueryTemplate = string.Empty;
            StringCollection viewFields = null;

            splistConfiguration = site.RootWeb.Lists.TryGetList(EFSimpleReminder.Configuration_ListTitle);
            if (splistConfiguration != null)
            {
                splistConfiguration.Delete();
                splistConfiguration = null;
            }
            if (splistConfiguration == null)
            {
                Guid guidConfiguration = site.RootWeb.Lists.Add(EFSimpleReminder.Configuration_ListTitle, EFSimpleReminder.Configuration_ListDescription, SPListTemplateType.GenericList);
                splistConfiguration = site.RootWeb.Lists[guidConfiguration];

                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ViewUrl, SPFieldType.Text, true);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSendTo, SPFieldType.Text, true);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderCC, SPFieldType.Text, false);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSubject, SPFieldType.Text, true);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderBody, SPFieldType.Note, false);
                SPFieldMultiLineText objSPFieldEmailBody = (SPFieldMultiLineText)splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderBody];
                objSPFieldEmailBody.RichText = false;
                objSPFieldEmailBody.RichTextMode = SPRichTextMode.Compatible;
                objSPFieldEmailBody.Update();
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_AppendHtmlTag, SPFieldType.Boolean, true);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_HtmlEncode, SPFieldType.Boolean, true);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_NotificationType, SPFieldType.MultiChoice, true);
                SPFieldMultiChoice objSPFieldNotificationType = (SPFieldMultiChoice)splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_NotificationType];
                objSPFieldNotificationType.Choices.Add(EFSimpleReminder.NotificationType_Email);
                objSPFieldNotificationType.Choices.Add(EFSimpleReminder.NotificationType_Task);
                objSPFieldNotificationType.DefaultValue = EFSimpleReminder.NotificationType_Email;
                objSPFieldNotificationType.Update();
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_TasksListTitle, SPFieldType.Text, false);
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_LastScanTime, SPFieldType.DateTime, false);
                SPFieldDateTime objSPFieldLastScanTime = (SPFieldDateTime)splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_LastScanTime];
                objSPFieldLastScanTime.DisplayFormat = SPDateTimeFieldFormatType.DateTime;
                objSPFieldLastScanTime.Update();
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderFrequency, SPFieldType.Choice, true);
                SPFieldChoice objSPFieldEmailFrequency = (SPFieldChoice)splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderFrequency];
                objSPFieldEmailFrequency.Choices.Add(EFSimpleReminder.ReminderFrequency_Everytime);
                objSPFieldEmailFrequency.Choices.Add(EFSimpleReminder.ReminderFrequency_OneTimeOnly);
                objSPFieldEmailFrequency.DefaultValue = EFSimpleReminder.ReminderFrequency_Everytime;
                objSPFieldEmailFrequency.Update();
                splistConfiguration.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_LastScanCount, SPFieldType.Number, false);

                splistConfiguration.EnableAttachments = false;
                splistConfiguration.Update();

                oViewConfiguration = splistConfiguration.DefaultView;
                oViewFieldsConfiguration = oViewConfiguration.ViewFields;
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_ViewUrl]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSendTo]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderCC]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSubject]);
                //oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_EmailBody]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_AppendHtmlTag]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_HtmlEncode]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_NotificationType]);
                oViewFieldsConfiguration.Add(splistConfiguration.Fields[EFSimpleReminder.Column_EFSimpleReminderCfg_LastScanCount]);

                oViewConfiguration.TabularView = false;
                oViewConfiguration.Update();

                viewFields = new StringCollection();
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ViewUrl);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSendTo);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSubject);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_AppendHtmlTag);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_HtmlEncode);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_AppendHtmlTag);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_NotificationType);
                viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderCfg_LastScanCount);

                strViewQueryTemplate = @"<Where><Eq><FieldRef Name='" + EFSimpleReminder.Column_EFSimpleReminderCfg_RecurringType + @"' /><Value Type='Choice'>{0}</Value></Eq></Where>";
                strViewQuery = string.Format(strViewQueryTemplate, EFSimpleReminder.RecurringType_Monthly);
                oViewConfiguration = splistConfiguration.Views.Add(EFSimpleReminder.RecurringType_Monthly, viewFields, strViewQuery, 30, true, false);
                oViewConfiguration.TabularView = false;
                oViewConfiguration.Update();
                strViewQuery = string.Format(strViewQueryTemplate, EFSimpleReminder.RecurringType_Weekly);
                oViewConfiguration = splistConfiguration.Views.Add(EFSimpleReminder.RecurringType_Weekly, viewFields, strViewQuery, 30, true, false);
                oViewConfiguration.TabularView = false;
                oViewConfiguration.Update();
                strViewQuery = string.Format(strViewQueryTemplate, EFSimpleReminder.RecurringType_Daily);
                oViewConfiguration = splistConfiguration.Views.Add(EFSimpleReminder.RecurringType_Daily, viewFields, strViewQuery, 30, true, false);
                oViewConfiguration.TabularView = false;
                oViewConfiguration.Update();
                strViewQuery = string.Format(strViewQueryTemplate, EFSimpleReminder.RecurringType_Hourly);
                oViewConfiguration = splistConfiguration.Views.Add(EFSimpleReminder.RecurringType_Hourly, viewFields, strViewQuery, 30, true, false);
                oViewConfiguration.TabularView = false;
                oViewConfiguration.Update();
                strViewQuery = string.Format(strViewQueryTemplate, EFSimpleReminder.RecurringType_Minutes);
                oViewConfiguration = splistConfiguration.Views.Add(EFSimpleReminder.RecurringType_Minutes, viewFields, strViewQuery, 30, true, false);
                oViewConfiguration.TabularView = false;
                oViewConfiguration.Update();

                //add test data
                SPListItem itemConfiguration = splistConfiguration.AddItem();
                itemConfiguration[SPBuiltInFieldId.Title] = @"EFSimpleReminder test";
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_ViewUrl] = @"/Lists/testList1/AllItems.aspx";
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSendTo] = @"xxx@gmail.com;[Created By]";
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderCC] = @"yyy@gmail.com;[Modified By]";
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderSubject] = @"Hello from EFSimpleReminder - Title=[Title]";
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_ReminderBody] = string.Format(@"This is a test email from EF{0}{0}ID=[ID],{0}Title=[Title],{0}Created=[Created],{0}Created By=[Created By],{0}Modified=[Modified],{0}ModifiedBy=[Modified By]", Environment.NewLine);
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_AppendHtmlTag] = true;
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_HtmlEncode] = true;
                itemConfiguration[EFSimpleReminder.Column_EFSimpleReminderCfg_TasksListTitle] = @"/Lists/Tasks/AllItems.aspx";
                itemConfiguration.Update();
            }

            //make sure the history list is already created
            splistHistory = site.RootWeb.Lists.TryGetList(EFSimpleReminder.History_ListTitle);
            if (splistHistory != null)
                splistHistory.Delete();

            Guid guidHistory = site.RootWeb.Lists.Add(EFSimpleReminder.History_ListTitle, EFSimpleReminder.History_ListDescription, SPListTemplateType.GenericList);
            splistHistory = site.RootWeb.Lists[guidHistory];
            splistHistory.EnableAttachments = false;
            splistHistory.Update();

            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_WebTitle, SPFieldType.Note, false);
            SPFieldMultiLineText objSPFieldWebTitle = (SPFieldMultiLineText)splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_WebTitle];
            objSPFieldWebTitle.RichText = false;
            objSPFieldWebTitle.RichTextMode = SPRichTextMode.Compatible;
            objSPFieldWebTitle.Update();
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ListTitle, SPFieldType.Text, false);
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ItemId, SPFieldType.Number, false);
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderSubject, SPFieldType.Text, false);
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderSendTo, SPFieldType.Text, false);
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderCC, SPFieldType.Text, false);
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ItemUrl, SPFieldType.Text, false);
            splistHistory.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_TaskUrl, SPFieldType.Text, false);

            oViewHistory = splistHistory.DefaultView;
            oViewFieldsHistory = oViewHistory.ViewFields;
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_WebTitle]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_ListTitle]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_ItemId]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderSubject]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderSendTo]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderCC]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_ItemUrl]);
            oViewFieldsHistory.Add(splistHistory.Fields[EFSimpleReminder.Column_EFSimpleReminderHistory_TaskUrl]);
            oViewHistory.TabularView = false;
            oViewHistory.Update();

            viewFields = new StringCollection();
            viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ListTitle);
            viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ItemId);
            viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderSubject);
            viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderHistory_ReminderSendTo);

            strViewQueryTemplate = @"<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";
            strViewQuery = string.Format(strViewQueryTemplate);
            oViewHistory = splistHistory.Views.Add(EFSimpleReminder.View_Portal, viewFields, strViewQuery, 30, true, false);
            oViewHistory.TabularView = false;
            oViewHistory.Update();

            //make sure the task list is already created
            splistTask = site.RootWeb.Lists.TryGetList(EFSimpleReminder.TaskDefault_ListTitle);
            if (splistTask != null)
                splistTask.Delete();

            splistTask = site.RootWeb.Lists[EFSimpleReminder.TaskDefault_ListTitle];
            if (splistTask != null)
            {
                viewFields = new StringCollection();
                viewFields.Add(splistTask.Fields[SPBuiltInFieldId.Modified].InternalName);
                viewFields.Add(splistTask.Fields[SPBuiltInFieldId.AssignedTo].InternalName);
                viewFields.Add(splistTask.Fields[SPBuiltInFieldId.Title].InternalName);
                //viewFields.Add(splistTask.Fields[SPBuiltInFieldId.Description].InternalName);

                strViewQueryTemplate = @"<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";
                strViewQuery = string.Format(strViewQueryTemplate);
                oViewTask = splistTask.Views.Add(EFSimpleReminder.View_Portal, viewFields, strViewQuery, 30, true, false);
                oViewTask.TabularView = false;
                oViewTask.Update();
            }

            //make sure the job list is already created
            splistJob = site.RootWeb.Lists.TryGetList(EFSimpleReminder.Job_ListTitle);
            if (splistJob != null)
                splistJob.Delete();

            Guid guidJob = site.RootWeb.Lists.Add(EFSimpleReminder.Job_ListTitle, EFSimpleReminder.Job_ListDescription, SPListTemplateType.GenericList);
            splistJob = site.RootWeb.Lists[guidJob];
            splistJob.Fields.Add(EFSimpleReminder.Column_EFSimpleReminderJob_AdminLink, SPFieldType.URL, false);
            splistJob.EnableAttachments = false;
            splistJob.Update();

            oViewJob = splistJob.DefaultView;
            oViewFieldsJob = oViewJob.ViewFields;
            oViewFieldsJob.Add(splistJob.Fields[EFSimpleReminder.Column_EFSimpleReminderJob_AdminLink]);
            oViewJob.Update();

            viewFields = new StringCollection();
            viewFields.Add(EFSimpleReminder.Column_EFSimpleReminderJob_AdminLink);

            oViewJob = splistJob.Views.Add(EFSimpleReminder.View_Portal, viewFields, strViewQuery, 30, true, false);
            oViewJob.TabularView = false;
            oViewJob.Update();
        }

        static void Main(string[] args)
        {
            try
            {
                using (SPSite objSPSite = new SPSite(@"http://hvsp2010a:8080"))
                {
                    initLists(objSPSite);
                    initJobs(objSPSite);

                    using (SPWeb objSPWeb = objSPSite.OpenWeb())
                    {
                        //delete admin page
                        SPList objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.DocLib_SitePages_Title);
                        if (objSPList != null)
                        {
                            foreach (SPListItem item in objSPList.Items)
                            {
                                if (item.File.Name.StartsWith(EFSimpleReminder.AdminPage_Name, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    item.Delete();
                                    break;
                                }
                            }
                        }
                        Console.WriteLine("Admin page deleted.");

                        initAdminPage(objSPSite);

                        Console.WriteLine("Completed. Press enter key to exit.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("ex.Message={0}, ex.StackTrace={1}", ex.Message, ex.StackTrace));
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}
